package logic

import (
	"2210a/practice/models/redisinit"
	"context"
	"fmt"
	"math/rand"
	"time"

	"2210a/practice/practicerpc/internal/svc"
	"2210a/practice/practicerpc/practicerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *practicerpc.SendSmsRequest) (*practicerpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	code := rand.Intn(9000) + 1000
	get := redisinit.ClientRedis.Get("number" + in.Mobile)
	if get.Val() != "" {
		return nil, fmt.Errorf("短信限流频率在一分钟只能发送一次")
	}
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	redisinit.ClientRedis.Set("code"+in.Source+in.Mobile, code, time.Minute*5)
	redisinit.ClientRedis.Set("number"+in.Mobile, "1", time.Minute*1)
	return &practicerpc.SendSmsResponse{}, nil
}
